package impl

import (
	"cn.bupt.byy/monitor/models"
	"context"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"log"
)

type MonitorServiceImpl struct {
}

func (m *MonitorServiceImpl) ListAll(c context.Context) *models.Byy {
	serviceList, err := coreV1.Services(namespace).List(c, metav1.ListOptions{})
	if err != nil {
		log.Fatal(err)
	}
	deploymentList, err := appV1.Deployments(namespace).List(c, metav1.ListOptions{})
	if err != nil {
		log.Fatal(err)
	}

	podList, err := coreV1.Pods(namespace).List(c, metav1.ListOptions{})
	if err != nil {
		log.Fatal(err)
	}

	res := &models.Byy{
		SvcName:    "",
		DeployName: "",
		Pods:       nil,
	}

	if len(serviceList.Items) != 0 {
		res.SvcName = serviceList.Items[0].Name
	}
	if len(deploymentList.Items) != 0 {
		res.DeployName = deploymentList.Items[0].Name
	}

	if len(podList.Items) != 0 {
		res.Pods = make([]models.PodInfo, len(podList.Items))
		for i, p := range podList.Items {
			res.Pods[i] = models.PodInfo{
				Name:     p.Name,
				NodeName: p.Spec.NodeName,
				NodeIp:   p.Status.HostIP,
				Status:   string(p.Status.Phase),
			}
			if p.ObjectMeta.DeletionTimestamp != nil {
				res.Pods[i].Status = "Terminating"
			}
		}
	}
	return res
}
